package com.moyuxi.sound_of_the_forest.ui.compose

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.AbsoluteRoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.moyuxi.sound_of_the_forest.entity.LocalMusicBean

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun AlbumOrArtistListCompose(
    navToScanMusic: () -> Unit,
    albumMap: MutableMap<String, MutableList<LocalMusicBean>>,
    navToAlbumOrArtis: (LocalMusicBean, List<LocalMusicBean>) -> Unit
) {
    if (albumMap.isEmpty()) {// 没有数据就去扫描
        Column(
            modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(onClick = { navToScanMusic.invoke() }) { Text(text = "扫描歌曲") }
        }
    } else {// 有数据就显示专辑列表
        val albumKeys = albumMap.keys
        LazyVerticalGrid(
            columns = GridCells.Fixed(2),
            modifier = Modifier.fillMaxWidth(),
        ) {
            items(albumKeys.size) {
                val name = albumKeys.elementAt(it)
                val list = albumMap.getValue(name)
                val bean = list[0]
                Card(shape = MaterialTheme.shapes.large,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                        .clickable { navToAlbumOrArtis.invoke(bean, list) }) {
                    Box(
                        modifier = Modifier.fillMaxWidth(),
                    ) {
                        MusicImageCompose(
                            filePath = bean.path,
                            Modifier
                                .fillMaxWidth()
                                .fillMaxSize()
                        )
                        Column(
                            modifier = Modifier
                                .align(Alignment.BottomCenter)
                                .fillMaxWidth()
                                .background(Color(255, 255, 255, 100)),
                            verticalArrangement = Arrangement.Bottom,
                            horizontalAlignment = Alignment.CenterHorizontally,
                        ) {
                            Text(text = name, modifier = Modifier.basicMarquee())
                            Text(text = "${list.size}首")
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun AlbumOrArtistListInfoComponent(
    titleText: String,
    path: String,
    localMusicBeans: List<LocalMusicBean>,
    backStack: () -> Unit,
    playingAllClick: (List<LocalMusicBean>) -> Unit,
    playingInBean: (LocalMusicBean) -> Unit,
    addToPlayingList: (LocalMusicBean) -> Unit,
    removeFromDataList: (LocalMusicBean) -> Unit
) {
    Column(
        modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally
    ) {    // 标题栏
        MyTopAppBarCompose(visible = true,
            navIconIsMenu = false,
            navClick = { backStack.invoke() }) {
            Column(
                modifier = Modifier
                    .background(MaterialTheme.colorScheme.primaryContainer)
                    .fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally
            ) {
                MusicImageCompose(
                    filePath = path,
                    modifier = Modifier
                        .fillMaxWidth(0.45f)
                        .padding(10.dp)
                        .clip(AbsoluteRoundedCornerShape(25))
                )
                Text(text = titleText, fontSize = 24.sp)
            }
        }
        SongListCompose(
            localMusicBeans, playingAllClick, playingInBean, addToPlayingList, removeFromDataList
        )
    }
}